/* 首页效果 */
$(document).ready(function(){
	$(".nav li a").hover(function(){
		$(".navimg",this).stop().animate({top:"-15px"},150);
		$(".navzi",this).stop().animate({top:"0"},150);
	},function(){
		$(".navimg",this).stop().animate({top:"-32px"},150);
		$(".navzi",this).stop().animate({top:"-25px"},150);
	});

	$('.recom li').width($(window).width()/6);
	$(window).resize(function() {
		$('.recom li').width($(window).width()/6);
	});
	
	$(".women li a").hover(function(){
		$(".womzi",this).stop().animate({bottom:"0"},150);
	},function(){
		$(".womzi",this).stop().animate({bottom:"-50px"},150);
	});
	
	//通告
	$(".ne_con a.picimg").hover(function() {
        $(".notl_hover",this).stop().animate({bottom:"0"},400);
	},function(){
		$(".notl_hover",this).stop().animate({bottom:"-100%"},400);
    });
})

/* 置顶 */
$(function(){
	  //首先将#back-to-top隐藏
	  $("#totop").hide();
	  //当滚动条的位置处于距顶部100像素以下时，跳转链接出现，否则消失
	  $(function(){
		  $(window).scroll(function(){
			  if ($(window).scrollTop()>300){
				  $("#totop").fadeIn();
			  }else{
				  $("#totop").fadeOut();
			  }
		  });
		  //当点击跳转链接后，回到页面顶部位置
		  $("#totop").click(function(){
			  $('body,html').animate({scrollTop:0},300);
			  return false;
		  });
	  });
});

/* //档期选择 
$().ready(function(){
	$('.time_select_click').click(function(){
		$(this).next().toggle();
	});
	
	$("#nianfen ul li").click(function(){
		$("#nian").html($(this).html());
		$("#total_nian").html($.trim($(this).html().replace("年",".")));
		$("#y").val($.trim($(this).html().replace("年","")));
		$('.select').hide();
	});
	
	$("#yuefen ul li").click(function(){
		$("#yue").html($(this).html());
		$("#total_yue").html($(this).html());
		$("#m").val($.trim($(this).html().replace("月","")));
		$(".time_select_button").click();
		$('.select').hide();
	});
	
	$('.span_hover').click(function(){
		//alert('1');
		$(this).hide();
		$(this).parent().css('position','relative');
		$(this).parent().css('height','auto');
		$(this).parent().css('left','0');
		$(this).parent().css('height','auto');
		$(this).parent().css('background','#26272b');
		$(this).parent().css('z-index','1000');
	});
	
	$(".piao_con .day:odd").addClass('bg_day');
})

// 档期选择弹出 
function alertWin(a){
	$('.popover-mask').show();
	$('.popover-mask').height($(document).height());
	$('.popover').slideDown(200);
}
function closeWin(a){
	$('.popover-mask').hide();
	$('.popover').slideUp(200);
}
*/

$('.TX-win-open').bind('click',function(){

	$('.popover-mask').show();
	$('.popover-mask').height($(document).height());
	$('.popover').slideDown(200);

});

$('.TX-win-close').bind('click',function(){
	$('.popover-mask').hide();
	$('.popover').slideUp(200);
});

function filt(a){
	var selectName = $(a).attr("name");
	$(a).addClass('licur').siblings().removeClass('licur');
	$(a).siblings().each(function(){
		var name = $(this).attr('name');
		if(name != undefined && name != "" && !isNaN(name) && name < selectName)
		{
			$(this).addClass('licur');
			$(this).children('i').css("left","0px");
			$(this).children('i').css("width","13px");
		}
	});
	$(a).children('i').css("left","0px");
	$(a).children('i').css("width","13px");
	$(a).siblings('input').val($(a).attr('name'));
}
	
	
/* login */	
jQuery.fn.customInput = function(){
	return $(this).each(function(){	
		if($(this).is('[type=checkbox],[type=radio]')){
			var input = $(this);
			
			// 使用输入的ID得到相关的标签
			var label = $('label[for='+input.attr('id')+']');
			
			// 包裹在一个div输入+标签
			input.add(label).wrapAll('<div class="custom-'+ input.attr('type') +'"></div>');
			
			// 必要的浏览器不支持：hover伪类的标签
			label.hover(
				function(){ $(this).addClass('hover'); },
				function(){ $(this).removeClass('hover'); }
			);
			
			//绑定自定义事件，触发它，绑定点击，焦点，模糊事件				
			input.bind('updateState', function(){	
				input.is(':checked') ? label.addClass('checked') : label.removeClass('checked checkedHover checkedFocus'); 
			})
			.trigger('updateState')
			.click(function(){ 
				$('input[name='+ $(this).attr('name') +']').trigger('updateState'); 
			})
			.focus(function(){ 
				label.addClass('focus'); 
				if(input.is(':checked')){  $(this).addClass('checkedFocus'); } 
			})
			.blur(function(){ label.removeClass('focus checkedFocus'); });
		}
	});
};

//单选框
//$(function(){
//	$('input').customInput();
//});

//初始化页面垂直居中
function center(){
	var winHeight = $(window).height();
	var bodyHeight = $(document.body).height();
	var heightGap = (winHeight - bodyHeight)/2;
	$('body.resize').css('padding-top', heightGap);
	$(window).resize(function() {
		winHeight = $(window).height();
		bodyHeight = $('body').height();
		var heightGap = winHeight - bodyHeight;
		if(heightGap > 5){
			$('body.resize').css('padding-top', heightGap/2);
		}else{
			$('body.resize').css('padding-top', 0);
		}
	})
}window.onload=center;


/* 会员中心 */
function helpClose(a){
	$(".help_notice").slideUp(200);
	$(".menu_head .m_prompt").remove();
}

/* 模特筛选 start */
$('.control_bar .button_less').bind("click",
function(e){ 
	var display = $('.filterBox').css('display')=="none"?"block":"none";
	$('.filterBox').css('display',display)
	e.stopPropagation();
	$(this).toggleClass("spred");
});



/* 2015.7.14上传头像 */
function previewImage(file)
{
	var MAXWIDTH = 422;
	var MAXHEIGHT = 422;
	var div = document.getElementById('localImag');
	var div100 = document.getElementById('small100');
	var div50 = document.getElementById('small50');
	var div30 = document.getElementById('small30');
	if (file.files && file.files[0])
	{
		div.innerHTML = '<img id=preview1>';
		var img = document.getElementById('preview1');
		img.onload = function(){
			trueWidth=img.offsetWidth;
			trueHeight=img.offsetHeight;
			var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
			img.width = rect.width;
			img.height = rect.height;
			//img.style.marginLeft = rect.left+'px';
		//	img.style.marginTop = rect.top+'px';
			
			var wRate=trueWidth/rect.width;
			var hRate=trueHeight/rect.height;
			img.hRate=hRate;
			img.hRate=hRate;
			
			$('#localImag').css('margin-left',rect.left);
			$('#localImag').css('margin-top',rect.top);
			$('#id_widthPro').val(wRate);
			$('#id_heightPro').val(hRate);
			
			var y2 = 0;
			var x2 = 0;
			if(img.width > img.height*MAXWIDTH/MAXHEIGHT)
			{
				y2 = img.height;
				x2 = img.height*MAXWIDTH/MAXHEIGHT;
			}
			else
			{
				x2 = img.width;
				y2 = img.width*MAXHEIGHT/MAXWIDTH;
			}
			$('#id_top').val(0);
			$('#id_left').val(0);
			$('#id_right').val(x2);
			$('#id_bottom').val(y2);
			
			var selection = {'x1':0,'x2':x2,'y1':0,'y2':y2,'width':x2,'height':y2};
			preview(img,selection);
			
			$("#localImag").imgAreaSelect({
				aspectRatio:'1:1',
				x1:0,
				y1:0,
				//x2: img.width,
				//y2: img.height,
				x2: x2,
				y2: y2,
				wRate: img.wRate,
				hRate: img.hRate,
				handles:true,
				onSelectEnd:function(img, selection) {
							console.info(selection);
							$('#id_top').val(selection.y1);
							$('#id_left').val(selection.x1);
							$('#id_right').val(selection.x2);
							$('#id_bottom').val(selection.y2);
						},
				fadeSpeed:200,//出来效果
				onSelectChange:preview //执行选择后执行的函数
			});
			
		}
		var reader = new FileReader();
		reader.onload = function(evt){img.src = evt.target.result;$('#small100_img').attr('src',evt.target.result);$('#small50_img').attr('src',evt.target.result);$('#small30_img').attr('src',evt.target.result);}
		reader.readAsDataURL(file.files[0]);
	}
	else
	{
		var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
		file.select();
		file.blur();
		var src =document.selection.createRange().htmlText;
		div.innerHTML = '<img id=preview1>';
		var img = document.getElementById('preview1');
		img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		
		div100.innerHTML = '<img id=small100_img>';
		var img100 = document.getElementById('small100_img');
		img100.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		
		div50.innerHTML = '<img id=small50_img>';
		var img50 = document.getElementById('small50_img');
		img50.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        
		div30.innerHTML = '<img id=small30_img>';
		var img30 = document.getElementById('small30_img');
		img30.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		
		trueWidth=img.offsetWidth;
		trueHeight=img.offsetHeight;
		var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
		status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
		div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;"+sFilter+src+"\"'></div>";
				
		$('#localImag').css('margin-left',rect.left);
		$('#localImag').css('margin-top',rect.top);
	
		$('#id_widthPro').val(trueWidth/rect.width);
		$('#id_heightPro').val(trueHeight/rect.height);
		
		
		$('#id_top').val(0);
		$('#id_left').val(0);
		$('#id_right').val(rect.width);
		$('#id_bottom').val(rect.height);
		
		$("#localImag").imgAreaSelect({
			aspectRatio:'1:1',
			x1:0,
			y1:0,
			x2: rect.width,
			y2: rect.height,
			handles:true,
			onSelectEnd:function(img, selection) {
						$('#id_top').val(selection.y1);
						$('#id_left').val(selection.x1);
						$('#id_right').val(selection.x2);
						$('#id_bottom').val(selection.y2);
					},
			fadeSpeed:200,//出来效果
			onSelectChange:preview //执行选择后执行的函数
		});
	}
	$('.upload_btn').hide();
	
}
function clacImgZoomParam( maxWidth, maxHeight, width, height ){
	var param = {top:0, left:0, width:width, height:height};
	if( width>maxWidth || height>maxHeight )
	{

		rateWidth = width / maxWidth;
		rateHeight = height / maxHeight;

		if( rateWidth > rateHeight )
		{
		param.width = maxWidth;
		param.height = Math.round(height / rateWidth);
		}
		else
		{
		param.width = Math.round(width / rateHeight);
		param.height = maxHeight;
		}
	}

	param.left = Math.round((maxWidth - param.width) / 2);
	param.top = Math.round((maxHeight - param.height) / 2);
	return param;
	
}
$(function(){
	// 给上传区添加onchang 事件，掉用函数setImagePreview()
	/*$("#Member_headimg").change(function() {
		setImagePreview('Member_headimg','preview','localImag',360,220);
	})*/
	if($("#localImag").length>0)
	{
		$("#localImag").imgAreaSelect({
			aspectRatio:'1:1',
			
			handles:true,
			onSelectEnd:function(img, selection) {
						$('#id_top').val(selection.y1);
						$('#id_left').val(selection.x1);
						$('#id_right').val(selection.x2);
						$('#id_bottom').val(selection.y2);
					},
			fadeSpeed:200,//出来效果
			onSelectChange:preview //执行选择后执行的函数
		});
	}

})
function adjust(el, selection) {
	var scaleX = $(el).width() / (selection.width || 1);
	var scaleY = $(el).height() / (selection.width || 1);
	$(el+' img').css({
		width: Math.round(scaleX*$('#localImag').width() ) + 'px',
		height: Math.round(scaleY*$('#localImag').height() ) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
	});
	var h=selection.y2-selection.y1;
	var w=selection.x2-selection.x1;

	$('#id_widthPro').val(trueWidth/w);
	$('#id_heightPro').val(trueHeight/h);
	
}
function preview(img, selection) {
	adjust('#small100', selection);
	adjust('#small50', selection);
	adjust('#small30', selection);
}
		
		
function select_avatar(){
	$('#avatar_id').click();
}
function uploadavatar(){

	$('#avatar_form').submit();
}
function submit_avatar(){
	//$('#rotation').val(value);
	alert($('#id_top').val())
	return false;
	alert('修改成功');
	$('#crop_form').submit();
} 
function photo_reset(){
	$('.upload_btn').show();
	$('#preview1').hide();
	$('#small100_img,#small50_img,#small30_img').attr('src','/assets/src/images/photo.png');
	$("#localImag").imgAreaSelect({
		remove:true
	});
};

//微信二维码
$(function(){
	$(".wx_qrcode").hide();
	$(".f_weixin,.wx_qrcode").hover(function(){
		$(".wx_qrcode").show();
	},function(){
		$(".wx_qrcode").hide();
	})
});


//个人页面菜单效果
$(function(){
	$(".memu_but,.memu_list").hover(function(){
		$(".memu_list").show();
	},function(){
		$(".memu_list").hide();
	});
});

//个人中心作品展示
$(function(){
	$(".sub_photo_type").click(function(){
		var flag = $(this).attr('data-flag');
		if(flag == 1)
		{
			$(this).attr('data-flag','2');
			$(this).parent().children('.clearfix').hide();
		}
		else
		{
			var key = $(this).attr('data-key');
			$(this).attr('data-flag','1');
			$(this).parent().children('.clearfix').show();
			$(this).parent().children('.less_photo_div').hide();
			$('.photo_hide_'+key).hide();
		}
	});
});

//查看更多
function morePhoto(key,obj)
{
	$(".photo_hide_"+key).show();
	$(obj).parent().parent().children('.more_photo_div').hide();
	$(obj).parent().parent().children(".less_photo_div").show();
}

//更少
function lessPhoto(key,obj)
{
	$(".photo_hide_"+key).hide();
	$(obj).parent().parent().children(".more_photo_div").show();
	$(obj).parent().parent().children(".less_photo_div").hide();
}